package com.xinxing.city.recovery.ui;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.BR;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.adapter.SetCateAdapter;
import com.xinxing.city.recovery.databinding.ActivitySetCategoryBinding;
import com.xinxing.city.recovery.entitys.Catalogs;
import com.xinxing.city.recovery.entitys.KernelAction;
import com.xinxing.city.recovery.entitys.KernelActions;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 品类设置
 */

public class SetCategoryActivity extends AppCompatActivity {
    private ActivitySetCategoryBinding binding;
    private SetCateAdapter cateAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_set_category);
        cateAdapter = new SetCateAdapter(R.layout.item_set_cate, BR.catalog);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rlvPl.setLayoutManager(layoutManager);
        binding.rlvPl.setAdapter(cateAdapter);

        init();
    }
    private void init(){
        String categoryStr = (String) SharedPreferencesUtils.getParam(this,"set_category","");
        if(!categoryStr.equals("")){
            Type dev = new TypeToken<List<Catalogs>>() {}.getType();
            Gson gson = new Gson();;
            List<Catalogs> catalogs = gson.fromJson(categoryStr,dev);
            cateAdapter.setmDatas(catalogs);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        List<Catalogs> catalogs = cateAdapter.getmDatas();
        if(catalogs!=null){
            Gson gson = new Gson();;
            SharedPreferencesUtils.setParam(this,"set_category",gson.toJson(catalogs));
            EventBus.getDefault().post(new KernelAction(KernelActions.INIT_FINISH, null));
        }
    }
}
